// pages/acceptIndex/acceptIndex.js
import api from '../../config/api';
import request from '../../utils/request';
import { shareOptions } from "../../utils/access";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 页面标题
    title: '',
    // 是否是空间页面
    isSpace: false,
    // 空间id
    spaceId: null,
    // 录音是否在播放中
    playing: false,
    // 列表数据
    list: [],
    // 详情数据
    data: null
  },

  // 页面id
  id: null,
  // 请求loading
  loading: false,
  // 音频播放
  audioPlayer: wx.createInnerAudioContext(),

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const isSpace = !!+options?.isSpace

    this.setData({
      title: isSpace ? '空间详情' : '容器详情',
    })

    this.id = options?.id
    this.setData({
      isSpace,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 播放录音&停止录音监听
    this.audioPlayer.onPlay(() => {
      this.setData({
        playing: true
      })
    })
    this.audioPlayer.onStop(() => {
      this.setData({
        playing: false
      })
    })
    this.audioPlayer.onEnded(() => {
      this.setData({
        playing: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList()
    this.getData()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData()
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    const { playing } = this.data
    if (playing) {
      this.audioPlayer.stop()
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    const { playing } = this.data
    if (playing) {
      this.audioPlayer.stop()
    }
  },
  // 获取列表
  async getList() {
    const { isSpace } = this.data
    const res = await request.get(
      isSpace ? api.containerList : api.goodsList,
      {
        [isSpace ? 'storageSpaceId' : 'containerId']: this.id
      }
    )
    this.setData({
      list: res?.data || []
    })
    wx.stopPullDownRefresh()
  },
  // 获取详情数据
  async getData() {
    const { isSpace } = this.data
    const res = await request.get(
      `${isSpace ? api.spaceDetail : api.containerDetail}/${this.id}`,
    )
    this.audioPlayer.src = res?.data?.remarkVoice
    this.setData({
      data: {
        ...res?.data,
      },
    })
  },
  // 播放录音
  play() {
    const { playing } = this.data
    if (playing) {
      this.audioPlayer.stop()
    } else {
      this.audioPlayer.play()
    }
  },
  // 跳转详情编辑页
  jumpDetail() {
    const { data, isSpace } = this.data
    wx.navigateTo({
      url: `/pages/addStorage/addStorage?isSpace=${+isSpace}&id=${data.id}`,
    })
  },
  // 跳转新增页
  jumpAdd() {
    const { data } = this.data
    wx.navigateTo({
      url: `/pages/addStorage/addStorage?spaceId=${data.id}`
    })
  },
  // 跳转编辑页
  jumpEdit(e) {
    const { isSpace, data } = this.data
    const item = e?.currentTarget.dataset.item || {}
    wx.navigateTo({
      url: isSpace
        ? `/pages/acceptIndex/acceptIndex?id=${item.id}`
        // ? `/pages/addStorage/addStorage?id=${item.id}&spaceId=${data.id}`
        : `/pages/acceptDetails/acceptDetails?id=${item.id}`,
    })
  },
  // 用户点击右上角分享
  onShareAppMessage(e) {
    return shareOptions('default',e)
  },
})